
//-------------------------------------------------------------
//-------------------------------------------------------------
group_edit.Instances = null;
//-------------------------------------------------------------
group_edit.CreateObject = function(config) {
    var obj = null;
    if (group_edit.Instances != null) {
        obj = group_edit.Instances[config.id];
    }
    if (obj == null) {
        if (group_edit.Instances == null) {
            group_edit.Instances = new Object();
        }
        obj = new group_edit(config);
        group_edit.Instances[config.id] = obj;
    }
    obj.init();

    return obj;
};
//-------------------------------------------------------------
function group_edit(config) {
    var Me = this;
    this.id = config.id;
    this.id_group = config.id_group;
    this.full_url = config.full_url;


    //------------------------------------------------------------
    
    this.init = function() {


        jQuery.validator.addMethod("Group", function(value, element) {

            return true;
        }, "This field is required.");

        var fb = true;
        $('#validation-form').validate({
            errorElement: 'div',
            errorClass: 'help-block',
            focusInvalid: false,
            rules: {
                namegroup: {
                    required: true
                },
                description: {
                    required: true
                },
                location_id: {
                    required: true
                },
                permission_id: {
                    required: true
                },
            },
            messages: {
            },
            invalidHandler: function(event, validator) { //display error alert on form submit   
                $('.alert-danger', $('.login-form')).show();
            },
            highlight: function(e) {
                if (fb) {
                    $(e).focus();
                    fb = false;
                }
                $(e).closest('.form-group').removeClass('has-info').addClass('has-error');
            },
            success: function(e) {
                $(e).closest('.form-group').removeClass('has-error').addClass('has-info');
                $(e).remove();

            },

            submitHandler: function(form) {
                //save
//                var id = jQuery(this).attr('itemid');
                _ajax.url = url_base + "admin/group/save_edit_group?id=" + Me.id_group;
                _ajax.data = jQuery(form).serialize();
                _ajax.loading(true);
               // jQuery('.wizard-actions button').hide();
                jQuery("#loading").show();
                _ajax.method = 'POST';
                _ajax.success = function(data) {
                    _ajax.loading(false);
                    if (data.error == 1) {
                        show_msg_error(data.msg);
                    } else {
//                             window.location = url_base + "admin/group/list";
                              show_msg_success(data.msg);
                            
                    }
                    jQuery("#loading").hide();
                };
                _ajax.show();
            },
        });
        $('#permission_id').multiSelect({
            selectableOptgroup: true
        });
        $('#selectallper').click(function() {
            $('#permission_id').multiSelect('select_all');
            return false;
        });
        $('#deselectallper').click(function() {
            $('#permission_id').multiSelect('deselect_all');
            return false;
        });        
        jQuery("#cmdeditgroup").click(function() {
           
            $('#validation-form').submit();
        });
    }


    //javascript code here
}


